'use strict';
(function() {
	var use = (function($) {
		var
		common = {
			originPositionData : null,
			submitButton : $('#submitButton'),
			form : $('#XnaForm'),
			status : $("#status").val(),
			init : function() {
				helper.date_ymd(true);
				helper.date_his();
				this.submit();
				this.findExist();
				this.responds();
			},
			submit : function() {
				var _this = this, msg;

				this.submitButton.click(function(e) {
					if (_this.fieldTest()) {

						$.post('/xna/' + _this.status, _this.form.serialize(), function(data) {
							console.log(data);
							if (data == "success") {

								layer.msg('保存成功！');
								window.location.href = $("#backBtn").attr("href");
							} else {
								helper.errorPop(data + '请检查网络或联系管理员');
							}
						});

					}
				});
			},
			fieldTest : function() {
				var inputs = $("input[type='text']");

				return helper.detect(inputs);
			},
			findExist : function() {
				var _this = this;

				$("input[name=order]").change(function() {
					var from_id = $(this).siblings("b").html().match(/([a-zA-Z]+.*)/)[1];

					$.post('/xna/find-exist', 'xna_id=' + from_id + this.value, function(data) {
						var obj = eval ("(" + data + ")"),
								string = '';

						_this.originPositionData = obj;
						_this.fillPosition(obj);
						
						$("#samplepositionelse-xna_density").val(obj.xna_density);
						$("#samplealter-origin_density").val(obj.xna_density);
						if (obj.pk_id != null) {
							$("#samplepositionelse-xna_volumn").val(obj.xna_volumn + "(剩余:" + obj.xna_volumn_left + "uL)");
						} else {
							$("#samplepositionelse-xna_volumn").val("");
						}

						if (obj.pk_id != null) {
							_this.addDisabled(true);
						} else {
							$("#samplepositionelse-xna_density").val($("#origin_density").val());
							$("#samplealter-origin_density").val($("#origin_density").val());
							_this.addDisabled(false);
						}
					});
				});
			},
			responds : function() {
				var _this = this;

				$("#changeBtn").click(function(e, showChangeBtn = false) {
					if ($("#samplepositionelse-xna_volumn").val() != "") {
						_this.addDisabled(false, false);
						if (!showChangeBtn) {
							$(this).css("display", "none");
							$("#cancelBtn").css("display", "inline-block");
						}
					}
				});

				$("#cancelBtn").click(function() {
					_this.addDisabled(true);
					$("#changeBtn").css("display", "inline-block");
					$(this).css("display", "none");
					_this.fillPosition(_this.originPositionData);
				});

				if (/update/.test($("#status").val())) {
					_this.addDisabled(false, false);
				}

				$("#samplealter-use_num").change(function() {
					var volumn_value = $("#samplepositionelse-xna_volumn").val(),
							volumn = volumn_value.match(/(\d+)uL/);

					volumn = (volumn != null) ? volumn : volumn_value.match(/(\d+)\D*/);
					volumn = (volumn != null) ? parseInt(volumn[1]) : 0;
					if (volumn - this.value <= 0) {
						$("#changeBtn").click(true);
						_this.fillPosition(null);
					}
				});

				$("#samplepositionelse-xna_fridge").change(function() {
					if (this.value == 0) {
						_this.fillPosition(null);
					}
				});
			},
			addDisabled : function(value, disableVolumn = true) {
				if (disableVolumn) {
					$("#samplepositionelse-xna_volumn").attr("disabled", value);
					$("#samplepositionelse-xna_density").attr("disabled", value);
				}
				$("#samplepositionelse-xna_fridge").attr("disabled", value);
				$("#samplepositionelse-case").attr("disabled", value);
				$("#samplepositionelse-case_a").attr("disabled", value);
				$("#samplepositionelse-case_b").attr("disabled", value);
				$("#samplepositionelse-xna_x").attr("disabled", value);
				$("#samplepositionelse-xna_y").attr("disabled", value);
				$("#samplepositionelse-xna_z").attr("disabled", value);
			},
			fillPosition : function(obj) {
				$("#samplepositionelse-xna_fridge").val((obj != null) ? (obj.xna_fridge != null ? obj.xna_fridge : 1) : 0);
				$("#samplepositionelse-case").val((obj != null) ? obj.case : 0);
				$("#samplepositionelse-case_a").val((obj != null) ? obj.case_a : 0);
				$("#samplepositionelse-case_b").val((obj != null) ? obj.case_b : 0);
				$("#samplepositionelse-xna_x").val((obj != null) ? obj.xna_x : 0);
				$("#samplepositionelse-xna_y").val((obj != null) ? obj.xna_y : 0);
				$("#samplepositionelse-xna_z").val((obj != null) ? obj.xna_z : 0);
			}
		},
		detail = {
			init : function() {
				this.show();
			},
			show : function() {
				var _this = this;

				$(".xna-m").click(function(e) {

					var td = $(this).find("td")[1],
							tdi = $(this).find("i"),
							from_id = $(td).html().match(/(.*)-M/)[1],
							thisTr = this;

					if ($(tdi).attr("class") == "fa fa-sort-asc") {
						$(tdi).attr("class", "fa fa-sort-desc");

						$.post('/xna/detail', 'from_id=' + from_id, function(data) {

								var obj = eval ("(" + data + ")"),
										string = '',
										j = 0;

								if (/-[A-D]/.test(from_id) && obj.alter != undefined) {
									for (var i = 0; i < obj.alter.length; i ++) {
										string += _this.stringAlter(obj.alter[i], 'rna');
									}
								} else {
									for (var i = 0; i < obj.data.length; i ++) {
										string += _this.stringData(obj.data[i]);
										if (obj.alter != undefined) {
											for (; j < obj.alter.length;) {
												if (obj.alter[j].from_id == obj.data[i].pk_id) {
													string += _this.stringAlter(obj.alter[j ++], 'dna');
												} else {
													break;
												}
											}
										}
									}
								}
	            	$(thisTr).after(string);
							});
					} else {
						$("i[class='fa fa-sort-desc']").attr("class", "fa fa-sort-asc");
						$(".xna-else").remove();
					}
				});
			},
			stringData : function(obj) {
				var fridge = [['使用结束', '-80°C', '4°C'], ['使用结束', '-20°C']],
						xna_type = (/-[A-D]/.test(obj.from_id)) ? 1 : 0,//1 is rna, 0 is dna
						string_plus = (obj.xna_volumn_left > 0) ? '<a href="/xna/update?id=' + obj.pk_id + '&type=xna"><span class="glyphicon glyphicon-pencil"></span></a>&nbsp;' + '<a href="/xna/create-dna?from=' + obj.from_id + '"><span class="glyphicon glyphicon-plus"></span></a>' : '',
						string = '<tr class="xna-else">' +
            						'<td></td>' +
            						'<td>' + obj.xna_id + '</td>' +
            						'<td' + (obj.xna_fridge != 0 ? '' : ' colspan=3 ') + '>' + fridge[xna_type][obj.xna_fridge] + '</td>' +
            						((obj.xna_fridge != 0) ? 
					             	'<td>' + obj.case + '</td>' +
					             	'<td>A(' + obj.case_a + ', ' + obj.case_b + ')&nbsp;X(' + obj.xna_x + ', ' + obj.xna_y + ', ' + obj.xna_z + ')</td>' : '') +
            						'<td>' + obj.xna_density + '</td>' +
            						'<td>' + obj.xna_volumn_left + '</td>' +
            						'<td></td><td></td>' +
            						'<td>' + obj.crt_name + '</td>' +
		        						'<td>' + obj.crt_time + '</td>' +
            						'<td>' + string_plus +
            						'</td>' +
            					'</tr>';
        return string;
			},
			stringAlter : function(obj, type) {
				var string = '<tr class="xna-else">' +
		        						'<td></td><td></td><td></td><td></td><td></td><td></td><td></td>' +
		        						'<td>' + obj.use_num + '</td>' +
		        						'<td>' + obj.use_name + '</td>' +
		        						'<td>' + obj.crt_name + '</td>' +
		        						'<td>' + obj.use_time + '</td>' +
		        						'<td><a href="/xna/update?id=' + obj.pk_id + '&type=alter&from=' + type + '"><span class="glyphicon glyphicon-pencil"></span></a></td>' +
		    							'</tr>';
		    return string;
			}
		},
		init = function() {
			common.init();
			detail.init();
		};
		return {
			init : init
		};
	})(jQuery);

	use.init();
})();